import { Image } from 'expo-image'; import { useLocalSearchParams, useRouter } from 'expo-router'; import React, { useCallback, useEffect, useRef, useState } from 'react'; import { ActivityIndicator, Dimensions, ImageBackground, ScrollView, StatusBar, StyleSheet, Text, TouchableOpacity, View } from 'react-native'; import { useSafeAreaInsets } from 'react-native-safe-area-context'; import { CheckoutModal } from '@/components/product/CheckoutModal'; import { Images } from '@/constants/images'; import { getGoodsDetail, GoodsDetail, previewSubmit } from '@/services/mall'; const { width: SCREEN_WIDTH } = Dimensions.get('window'); export default function ProductDetailScreen() { const { id, subjectId } = useLocalSearchParams<{ id: string; subjectId?: string }>(); const router = useRouter(); const insets = useSafeAreaInsets(); const checkoutRef = useRef(null); const [loading, setLoading] = useState(true); const [data, setData] = useState(null); // 加载商品详情 const loadData = useCallback(async () => { if (!id) return; setLoading(true); try { const detail = await getGoodsDetail(id, subjectId); setData(detail); } catch (error) { console.error('加载商品详情失败:', error); } setLoading(false); }, [id, subjectId]); useEffect(() => { loadData(); }, [loadData]); // 显示结算弹窗 const showCheckout = async () => { if (!data) return; try { const preview = await previewSubmit({ goodsId: id!, quantity: 1, subjectId, }); if (preview) { checkoutRef.current?.show(preview); } } catch (error) { console.error('预提交失败:', error); } }; // 返回 const goBack = () => { router.back(); }; if (loading) { return ( ); } if (!data) { return ( 商品不存在 ); } return ( {/* 顶部导航 */} 商品详情 {/* 商品图片 */} {/* 商品信息 */} ¥ {data.subjectPrice || data.price} {data.sellType === 2 && ( 预售 )} {data.spu.name} {/* 商品详情图片 */} {data.spu.images && data.spu.images.length > 0 && ( 商品详情 {data.spu.images.map((img, index) => ( ))} )} {/* 推荐商品 */} {data.recommendedMallGoods && data.recommendedMallGoods.length > 0 && ( 推荐商品 {data.recommendedMallGoods.map((item) => ( router.push(`/product/${item.id}` as any)} > {item.name} ¥{item.price} ))} )} {/* 底部购买栏 */} 客服 {data.sellType === 2 ? '支付定金' : '立即购买'} {/* 结算弹窗 */} ); } const styles = StyleSheet.create({ container: { flex: 1, backgroundColor: '#1a1a2e', }, background: { flex: 1, }, loadingContainer: { flex: 1, backgroundColor: '#1a1a2e', justifyContent: 'center', alignItems: 'center', }, errorText: { color: '#999', fontSize: 16, }, header: { flexDirection: 'row', alignItems: 'center', justifyContent: 'space-between', paddingHorizontal: 15, paddingBottom: 10, }, backBtn: { width: 40, height: 40, justifyContent: 'center', alignItems: 'center', }, backText: { color: '#fff', fontSize: 24, }, headerTitle: { color: '#fff', fontSize: 18, fontWeight: '600', }, placeholder: { width: 40, }, scrollView: { flex: 1, }, imageWrapper: { width: SCREEN_WIDTH, height: SCREEN_WIDTH, backgroundColor: '#fff', }, coverImage: { width: '100%', height: '100%', }, infoSection: { padding: 15, marginHorizontal: 10, marginTop: 10, borderRadius: 12, overflow: 'hidden', }, priceRow: { flexDirection: 'row', alignItems: 'baseline', }, currency: { color: '#ff6b00', fontSize: 14, }, price: { color: '#ff6b00', fontSize: 28, fontWeight: 'bold', }, presellTag: { backgroundColor: '#8b3dff', borderRadius: 12, paddingHorizontal: 10, paddingVertical: 3, marginLeft: 10, }, presellText: { color: '#fff', fontSize: 12, }, name: { color: '#333', fontSize: 16, marginTop: 10, lineHeight: 22, }, detailSection: { marginTop: 10, marginHorizontal: 10, padding: 15, borderRadius: 12, overflow: 'hidden', }, sectionTitle: { color: '#333', fontSize: 16, fontWeight: '600', marginBottom: 15, }, detailImage: { width: SCREEN_WIDTH - 50, height: 300, marginBottom: 10, }, recommendSection: { marginTop: 10, marginHorizontal: 10, padding: 15, borderRadius: 12, overflow: 'hidden', }, recommendItem: { width: 120, marginRight: 10, }, recommendImage: { width: 120, height: 120, borderRadius: 8, backgroundColor: '#fff', }, recommendName: { color: '#333', fontSize: 12, marginTop: 8, }, recommendPrice: { color: '#ff6b00', fontSize: 14, fontWeight: '600', marginTop: 4, }, bottomSpace: { height: 100, }, bottomBar: { position: 'absolute', bottom: 0, left: 0, right: 0, flexDirection: 'row', alignItems: 'center', paddingHorizontal: 15, paddingTop: 10, backgroundColor: 'rgba(0,0,0,0.3)', }, serviceBtn: { paddingHorizontal: 25, paddingVertical: 12, backgroundColor: 'rgba(255,255,255,0.9)', borderRadius: 25, }, serviceBtnText: { color: '#333', fontSize: 14, }, buyBtn: { flex: 1, marginLeft: 15, height: 45, overflow: 'hidden', }, buyBtnBg: { width: '100%', height: '100%', justifyContent: 'center', alignItems: 'center', }, buyBtnText: { color: '#fff', fontSize: 16, fontWeight: '600', }, });